<?php
namespace api\controllers;
use Yii;
class CurlController extends AuthController{
    protected $modelServie;

    public function actionIndex(){
        if(empty($this->modelServie)){
            return ['message'=>'請求方式出錯'];
        }
        
        $param = [];
        $param['limit'] = Yii::$app->request->get('limit');
        $param['page'] = Yii::$app->request->get('page');
        $param['pid'] = Yii::$app->request->get('pid', false);
        $param['type'] = Yii::$app->request->get('type');
        $searchParams = Yii::$app->request->get('searchParams');
        if(!empty($searchParams)){
            $searchParams = json_decode($searchParams, true);
            $param = \yii\helpers\ArrayHelper::merge($param, $searchParams);
        }
        if($param['pid'] === false){
            unset($param['pid']);
        }
        return $this->modelServie->getList($param);
    }
    public function actionView(){
        if(!empty($this->modelServie)){
            return $this->modelServie->detail(Yii::$app->request->get('id'));
        }
        return ['message'=>'請求方式出錯'];
    }
    public function actionCreate(){
        if(Yii::$app->request->isPost && !empty($this->modelServie)){
            return $this->modelServie->create(Yii::$app->request->post());
        }
        return ['message'=>'請求方式出錯'];
    }

    public function actionUpdate(){
        if(Yii::$app->request->isPost && !empty($this->modelServie)){
            return $this->modelServie->update(Yii::$app->request->post());
        }
        return ['message'=>'請求方式出錯'];
    }
    
    public function actionDelete(){
        if(Yii::$app->request->isPost && !empty($this->modelServie)){
            return $this->modelServie->delete(Yii::$app->request->post('id'));
        }
        return ['message'=>'請求方式出錯'];
    }
}